<?php 
class Country_Widget_ListFriendsController extends Engine_Content_Widget_Abstract{
    public function indexAction(){
        $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
        if(!$viewer || !$viewer->getIdentity()){
            return $this->setNoRender();
        }
        $viewer_id = $viewer->getIdentity();
        if( !Engine_Api::_()->core()->hasSubject() ) {
          return $this->setNoRender();
        }
        
        $this->view->subject = $subject = Engine_Api::_()->core()->getSubject('country');
        $country_id = $subject->getIdentity();
        $request = Zend_Controller_Front::getInstance()->getRequest();
        $form = new Country_Form_Member_Search();
        $this->view->form = $form;
        $countrymemberTb = Engine_Api::_()->getDbTable('countrymembers','country');
        $membershipTb = Engine_Api::_()->getDbtable('membership', 'user');
        
        /*$select  = $countrymemberTb->select()
                                    //->distinct()
                                    ->setIntegrityCheck(false)
                                    ->from('engine4_country_countrymembers as a',"a.*")
                                    ->join("engine4_user_membership as b","a.user_id = b.user_id",null)
                                    ->where("b.active = 1")
                                    ->where("a.country_id = $country_id")
                                    ->where("a.user_approved = 1")
                                    ->where("a.user_id = !$viewer_id")
                                    ->where("b.resource_id = $viewer_id")
                                    
                                    ; */
        $select_friends = $membershipTb->select()
                                        ->from("engine4_user_membership","user_id")
                                        ->where("resource_id = $viewer_id")
                                        ->where("active = 1");
        $friends_result = $membershipTb->fetchAll($select_friends);
        $friend_ids = array();
        $friend_ids[0] = 0;
        if(count($friends_result) > 0){
            
            $friends_result_array = $friends_result->toArray();
            foreach($friends_result_array as $friend){
                $friend_ids[] = $friend['user_id'];
            }
        }
        
        $select  = $countrymemberTb->select()
                                    ->where("country_id = ?", $country_id)
                                    ->where("user_approved = 1")
                                    ->where("user_id in (?)", $friend_ids);
        
        $userTb = Engine_Api::_()->getDbTable('users','user');
        if($request->isPost() && $form->isValid($request->getPost())){
            $username = $form->getValue('username');
            $email = $form->getValue('email');
            $select_user = $userTb->select();
            if(!empty($username)){
                $select_user = $select_user->where("displayname LIKE '%{$username}%'");
            }
            if(!empty($email)){
                    $select_user = $select_user->where('email = ?', $email);
            }
            
            $list_users = $userTb->fetchAll($select_user);
            $user_ids = array();
            $user_ids[0] = 0;
            foreach($list_users as $user){
                $user_ids[] = $user->getIdentity();
            }
            $this->view->active_div = true;
            $select = $select->where('user_id in (?)', $user_ids);
        }
        
        $this->view->members = $members = $countrymemberTb->fetchAll($select);
    }
}
?>